package com.cuz.for2024;

public class T6 {
    public String convert(String s, int numRows) {
        if (s == null || s.length() <= numRows||numRows<=1) {
            return s;
        }
        StringBuilder[] buffers = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            buffers[i] = new StringBuilder();
        }
        boolean down = true;
        char[] charArray = s.toCharArray();

        for (int i = 0; i < charArray.length; ) {
            int tempCount = 0;
            while (tempCount < numRows - 1 && i + tempCount < charArray.length) {
                char cur = charArray[i + tempCount];
                int bIndex = down ? tempCount : numRows - tempCount - 1;
                buffers[bIndex].append(cur);
                tempCount++;
            }
            down = !down;
            i += numRows - 1;
        }
        StringBuilder r = buffers[0];
        for (int i = 1; i < numRows; i++) {
            r.append(buffers[i]);
        }
        return r.toString();
    }

    public static void main(String[] args) {
        System.out.println(new T6().convert("PAYPALISHIRING", 4));
    }
}
